package com.ytjj.quartz.service;

import com.ytjj.qmyx.mall.mapper.SpecialRankMapper;
import com.ytjj.qmyx.mall.model.Product;
import com.ytjj.qmyx.mall.model.SpecialRank;
import com.ytjj.quartz.dao.ProductDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class SpecialRankService {
    @Resource
    private SpecialRankMapper specialRankMapper;
    @Resource
    private ProductDao productDao;

    public int insert(SpecialRank record) {
        return specialRankMapper.insert(record);
    }

    public int insertSelective(SpecialRank record) {
        return specialRankMapper.insertSelective(record);
    }

    public void deleteBySpecialId(Integer specialId) {
        if (null == specialId) {
            return;
        }
        specialRankMapper.deleteBySpecialId(specialId);
    }

    public List<SpecialRank> selectBySpecialId(Integer specialId) {
        return specialRankMapper.selectBySpecialId(specialId,"h5",10);
    }

    public void generateDataByTask(List<Integer> categoryIds, Integer specialId, String specialTitle) {
        List<Product> productList = productDao.selectProductByThirdId(categoryIds, "sale", Arrays.asList("CASH_SCORE"));
        for (int i = 0; i < productList.size(); i++) {
            Product product = productList.get(i);
            SpecialRank record = new SpecialRank();
            record.setSpecialId(specialId);
            record.setSpecialTitle(specialTitle);
            record.setProductId(product.getId());
            record.setProductName(product.getProductName());
            record.setProductImg(product.getProductImg());
            record.setVolume(Integer.parseInt(product.getVolumeStr()));
            record.setSort(i + 1);
            specialRankMapper.insert(record);
        }
    }
}
